<?php

class HttpClient{
    /*
     * @author  vincent ln1394@163.com
     * @date 2017-9-25
     */

    public static function post($url, $post_data = '', $timeout = 5){//curl
        $headers[] = "Accept: application/json";
        $headers[] = "Content-Type: application/json";
        $headers[] = "charset: utf-8";

        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        if($post_data != ''){
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $file_contents = curl_exec($ch);
        //var_dump($file_contents);
        curl_close($ch);
        return json_decode($file_contents,1);

    }
}
